package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.Orders;
import com.itheima.service.OrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/userPage")
    public R queryUserPage(Integer page,Integer pageSize){

        // select * from orders where user_id = ?
        IPage<Orders> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        qw.eq(Orders::getUserId,userId);
        orderService.page(pageInfo,qw);

        long total = pageInfo.getTotal();
        List<Orders> records = pageInfo.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);

        return R.success(map);
    }


    @GetMapping("/page")
    public R queryPage(Integer page,Integer pageSize){

        IPage<Orders> pageInfo = new Page<>(page,pageSize);
        orderService.page(pageInfo);
        long total = pageInfo.getTotal();
        List<Orders> records = pageInfo.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);

        return R.success(map);
    }
    @PostMapping("/submit")
    public R submitOrder(@RequestBody Orders orders){

        orderService.submit(orders);

        return R.success("下单成功，请愉快用餐！");
    }

}
